﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shape
{
    //抽象类 图形
    abstract class Shape
    {
        public abstract double GetArea();
    }
    //判断形状是否存在
    interface IReasonableShape
    {
        bool ShapeExist();
    }
    //长方形
    class Rectangle: Shape, IReasonableShape
    {
        private double _width;
        private double _height;
        public double Width { get => _width; }
        public double Height { get => _height; }
        public Rectangle(double width, double height)
        {
            _width = width;
            _height = height;
        }
        public Rectangle()
        {
            AutoDate();
        }
        public override double GetArea()
        {
            double area;
            area = this._height * _height;
            return ShapeExist()? area:Double.NaN;
        }
        public bool ShapeExist()
        {
            if (_width <= 0 || _height <= 0)
            {
                Console.WriteLine("长方形不存在");
                return false;
            }
            return true;
        }
        private void AutoDate()
        {
            Random random = new Random();
            this._width = random.Next(10, 1000) / 10;
            this._height = random.Next(10, 1000) / 10;
        }
    }
    //正方形
    class Square: Rectangle
    {
        private double _side;
        public double Side { get => _side; }
        public Square(double side) : base(side, side)
        {
            _side = side;
        }
        public Square()
        {
            AutoDate();
        }
        private void AutoDate()
        {
            Random random = new Random(42);
            this._side = random.Next(10, 1000) / 10;
        }
    }
    //三角形
    class Triangle:Shape, IReasonableShape 
    {
        private double side1;
        private double side2;
        private double side3;
        public double Side1 { get => side1; }
        public double Side2 { get => side2; }
        public double Side3 { get => side3; }
        public Triangle(double side1,double side2,double side3)
        {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }
        public Triangle()
        {
            AutoDate();
        }
        public override double GetArea()
        {
            if (ShapeExist())
            {
                double p = (side1 + side2 + side3) / 2;
                double area;
                area = Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3));
                return area;
            }
            else return Double.NaN;
        }
        public bool ShapeExist()
        {
            if (side1 <= 0 || side2 <= 0 || side3 <= 0 || side1 >= side2 + side3 || side2 >= side1 + side3 || side3 >= side1 + side2)
            {
                Console.WriteLine("三角形不存在");
                return false;
            }
            return true;
        }
        private void AutoDate()
        {
            while (side1 >= side2 + side3 || side2 >= side1 + side3 || side3 >= side1 + side2)
            {
                Random random = new Random();
                this.side1 = random.Next(10, 1000) / 10;
                this.side2 = random.Next(10, 1000) / 10;
                this.side3 = random.Next(10, 1000) / 10;
            }
        }
    }
    //简单工厂模式
    class Factory
    {
        public static Shape CreateShape(int i) 
        {
            Shape shape = null;
            switch (i)
            {
                case 0:
                    shape = new Rectangle();
                    Console.WriteLine("创建了一个长方形");
                    break;
                case 1:
                    shape = new Square();
                    Console.WriteLine("创建了一个正方形");
                    break;
                case 2:
                    Console.WriteLine("创建了一个三角形");
                    shape = new Triangle();
                    break;
                default:
                    break;
            }
            return shape;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            double Area = 0;
            int[] num = new int[10];
            Random random = new Random();
            for (int i = 0; i <= 9; i++)
            {
                num[i] = random.Next(3);
            }
            for(int i = 0; i < 9; i++)
            {
                Shape shape = Factory.CreateShape(num[i]);
                Console.WriteLine("面积为：" + shape.GetArea());
                Area += shape.GetArea();
            }
            Console.WriteLine("十个随机图行面积之和：" + Area);
            return;
        }
    }
}
